<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一个数组，用一个数组的值作为其键名，另一个数组的值作为其值</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-combine" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_combine</h1>
    <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_combine</span> &mdash; <span class="dc-title">
     创建一个数组，用一个数组的值作为其键名，另一个数组的值作为其值
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-combine-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>array_combine</strong></span>
       ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$keys</code></span>
      , <span class="methodparam"><span class="type">array</span> <code class="parameter">$values</code></span>
      ) : <span class="type">array</span></div>

    <p class="para rdfs-comment">
     返回一个 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，用来自
     <code class="parameter">keys</code> 数组的值作为键名，来自
     <code class="parameter">values</code> 数组的值作为相应的值。
    </p>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.array-combine-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>


    
     <dt>
<code class="parameter">keys</code></dt>

     <dd>

      <p class="para">
       将被作为新数组的键。非法的值将会被转换为字符串类型（<span class="type"><a href="language.types.string.html" class="type string">string</a></span>）。
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">values</code></dt>

     <dd>

      <p class="para">
       将被作为 <span class="type"><a href="language.types.array.html" class="type Array">Array</a></span> 的值。
      </p>
     </dd>

    

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-combine-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回合并的 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，如果两个数组的单元数不同则返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.array-combine-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果作为<code class="parameter">keys</code>的数组和作为<code class="parameter">values</code>的数组的元素个数不一样，将会抛出一个警告错误（<strong><code>E_WARNING</code></strong>）。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-combine-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.4.0</td>
       <td>
        （修复）早期版本中如果是空数组就报<strong><code>E_WARNING</code></strong>的错并且返回<strong><code>FALSE</code></strong>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.array-combine-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6017">
    <p><strong>Example #1 一个 <span class="function"><strong>array_combine()</strong></span> 简单的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'avocado'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_combine</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [green]  =&gt; avocado
    [red]    =&gt; apple
    [yellow] =&gt; banana
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-combine-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_merge.html" class="function" rel="rdfs-seeAlso">array_merge()</a> - 合并一个或多个数组</span></li>
    <li class="member"><span class="function"><a href="array_walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - 使用用户自定义函数对数组中的每个元素做回调处理</span></li>
    <li class="member"><span class="function"><a href="array_values.html" class="function" rel="rdfs-seeAlso">array_values()</a> - 返回数组中所有的值</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>